<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2010 Grzegorz Bańka <grzegorz.banka@gmail.com>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 * Hint: use extdeveval to insert/update function index above.
 */

require_once(PATH_tslib.'class.tslib_pibase.php');
require_once(PATH_t3lib."class.t3lib_tcemain.php");
require_once (PATH_t3lib.'class.t3lib_pagetree.php');

/**
 * Plugin 'J24h Panel' for the 'jh24_panel' extension.
 *
 * @author	Grzegorz Bańka <grzegorz.banka@gmail.com>
 * @package	TYPO3
 * @subpackage	tx_jh24panel
 */
class tx_jh24panel_pi1 extends tslib_pibase {
	var $prefixId      = 'tx_jh24panel_pi1';		// Same as class name
	var $scriptRelPath = 'pi1/class.tx_jh24panel_pi1.php';	// Path to this script relative to the extension dir.
	var $extKey        = 'jh24_panel';	// The extension key.
	var $pi_checkCHash = true;
	
	/**
	 * The main method of the PlugIn
	 *
	 * @param	string		$content: The PlugIn content
	 * @param	array		$conf: The PlugIn configuration
	 * @return	The content that is displayed on the website
	 */
	function main($content, $conf)	{
		$this->conf=$conf;
		return $this->panel();
	}
	function panel()
	{
		if(isset($_GET['clearmeCache'])) 
		{
			if(!isset($_GET['clearall'])) $this->clearCache();
			else {
			
				$alluid=intval($_GET['clearall']);
				$alluid=$this->get_pagetree($alluid);
				
				for($i=0; $i<count($alluid); $i++)
				{
					$newuid=$alluid[$i]['row']['uid'];		
					//print_r($newuid.','); 
					$this->clearCache($newuid);						
				}
				$this->clearCache($this->conf['root_uid']);		
			}
			if(!isset($_GET['this_pageuid']))
			{
				$url=str_replace('&clearmeCache=1','',$_SERVER['REQUEST_URI']);
				header('Location: '.$url);
				exit;
			}
			else
			{
			
				$url=$_SERVER['SERVER_NAME'].'?id='.intval($_GET['this_pageuid']);
				header('Location: http://'.$url);
				exit;
			}
		}
		$content=$this->nav();
		$content='<div id="jh_panel">'.$content.'</div>';

		return $content;
	}

	function loadTS($pageUid) {
	   $sysPageObj = t3lib_div::makeInstance('t3lib_pageSelect');
	   $rootLine = $sysPageObj->getRootLine($pageUid);
	   $TSObj = t3lib_div::makeInstance('t3lib_tsparser_ext');
	   $TSObj->tt_track = 0;
	   $TSObj->init();
	   $TSObj->runThroughTemplates($rootLine);
	   $TSObj->generateConfig();
	   return $TSObj->setup;
	}
	
	function nav()
	{
		$cat='';
		$file=$GLOBALS['BE_USER']->user['file_mountpoints'];		
		$news_page=explode(',',$this->conf['news_page']);
		$setup=$this->loadTS(intval($GLOBALS['TSFE']->id));
		
		$dam=$setup['plugin.']['tx_jh24panel_pi1.']['dam'];
		$add_file=$this->add_file($file,$dam);

		if($setup['plugin.']['tx_jh24panel_pi1.']['add_ttaddress']==1)
		{
			$pid=$GLOBALS['TSFE']->id;
			$news_list='
			<a href="/typo3/alt_doc.php?returnUrl='.urlencode($_SERVER['REQUEST_URI']).'&edit[tt_address]['.$pid.']=new">Dodaj nowy adres</a> lub 
			<a href="typo3/db_list.php?id='.$pid.'&table=tt_address">Edytuj aders z listy adresów</a>
			';
		}

		if(in_array($GLOBALS['TSFE']->id,$news_page) || in_array('*',$news_page))
		{
			if($this->conf['pid']=='') $pid=$setup['plugin.']['tt_news.']['pid_list'];
			else $pid=$this->conf['pid']; 
			
			if($setup['plugin.']['tx_jh24panel_pi1.']['add_cat']!='' && $setup['plugin.']['tx_jh24panel_pi1.']['use_only_get_cat']!=1) $addcat[]=$setup['plugin.']['tx_jh24panel_pi1.']['add_cat'];
		  	if($setup['plugin.']['tx_jh24panel_pi1.']['get_cat']==1 && isset($_GET['tx_ttnews']['cat'])) $addcat[]=$_GET['tx_ttnews']['cat'];			
			$cat='&defVals[tt_news][category]='.implode(',',$addcat);
			
		//	print_r($this->conf['cat_only_parent']);
			
			if($this->conf['cat_only_parent']!='') 
			{
				$addcat=$this->cat_only_parent($addcat,$this->conf['cat_only_parent']);
				if(!isset($_GET['tx_ttnews']['cat']) && $addcat[0]=='') $addcat[]=$this->conf['cat_only_parent'];
			}
			//$cat='data[tt_news][410][		
			
			if($this->conf['add_parameter']=='') $news_list='<a href="/typo3/alt_doc.php?returnUrl='.urlencode($_SERVER['REQUEST_URI']).'&edit[tt_news]['.$pid.']=new'.$cat.'" id="mt_add_ttnews"><span></span>'.$this->conf['add_text'].'</a>';
			else if($addcat[0]!='') $news_list='<a href="/typo3/alt_doc.php?returnUrl='.urlencode($_SERVER['REQUEST_URI']).str_replace(array('###PID###','###CAT###'),array($pid,implode(',',$addcat)),$this->conf['add_parameter']).'" id="mt_add_ttnews"><span></span>'.$this->conf['add_text'].'</a>';
		}
		if(isset($_GET['tx_ttnews']['tt_news'])) $news_list ='<a href="/typo3/alt_doc.php?returnUrl='.urlencode($_SERVER['REQUEST_URI']).'&edit[tt_news]['.$_GET['tx_ttnews']['tt_news'].']=edit" id="mt_edit_ttnews"><span></span>Edytuj treść</a>';
		$add[]='
			<div id="mt_bemenu">
				<a href="/typo3/" target="_blank" title="Open TYPO3 CMS in new window" id="backto_typo3"></a>
				'.$news_list.'
			</div>
			';
		$add[]=$add_file;
		$add[]=$this->get_www();
		//$add[]='<div id="clearcache"><input name="clearmeCache" type="submit" value="Czyść Cache" onClick="return confirm(\'Czy na pewno chcesz usunąć cache dla tej strony?\')" /></div>';
		//$add[]='<a href="'.$_SERVER['REQUEST_URI'].'&clearmeCache=1" onClick="return confirm(\'Czy na pewno chcesz usunąć cache dla tej strony?\')"><span id="mtcache"></span>Usuń cache</a>';
				
		$start='<form action="'.$_SERVER['REQUEST_URI'].'" method="post">';
		$end='</form>';
		return $start.implode("\n",$add).$end;
	}
	function cat_only_parent($allcat,$parent)
	{
		$where = 'hidden=0 AND deleted=0 AND parent_category IN('.$parent.')';
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid','tt_news_cat',$where);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			if(in_array($row['uid'],$allcat)) $newall[]=$row['uid'];
		}			
		return $newall;
	}
	function get_www()
	{
		$where = 'hidden=0 AND deleted=0 AND is_siteroot=1';
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid','pages',$where);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{	
			$www[]=$this->get_domain($row['uid'],$row['pid'])==false?'':$this->get_domain($row['uid'],$row['pid']);
		}
		return '<div id="mtwwwme"><a href="#"><span></span>WWW</a><ul>'.implode("\n",$www).'</ul></div>';
	}
	function get_domain($uid,$pid)
	{
		$where = 'hidden=0 AND deleted=0 AND uid='.$pid;
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('title','pages',$where);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{	
			$title=$row['title'];
		}
		$where = 'hidden=0 AND pid='.$uid;
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('domainName 	','sys_domain',$where);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{	
			$domainName=$row['domainName'];
		}
		if($title!='') return '<li><a href="http://'.$domainName.'">'.$title.'</a></li>';
		else return false;
		
	}
	function clearCache($id='')
	{
		//print_r('aaaaaaa');

		if($id=='') $id=intval($GLOBALS['TSFE']->id);
		$tce = t3lib_div::makeInstance('t3lib_TCEmain');
		$tce->stripslashes_values = 0;
		$tce->start(array(), array());
		$tce->clear_cacheCmd($id);

		//print_r('ffffffff');
	}
	function get_tx_templavoila_to($uid,$next=0)
	{
		
		$where = 'hidden=0 AND deleted=0 AND uid='.$uid;
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_templavoila_to,tx_templavoila_next_to,pid','pages',$where);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			if($next==0)
			{
				if($row['tx_templavoila_to']=='' || $row['tx_templavoila_to']==0) return $this->get_tx_templavoila_to($row['pid'],1);
				else return $row['tx_templavoila_to'];
			}else 
			{
				if($row['tx_templavoila_next_to']!='' &&  $row['tx_templavoila_to']!=0) return $row['tx_templavoila_next_to'];
				elseif($row['tx_templavoila_to']=='' || $row['tx_templavoila_to']==0) return $this->get_tx_templavoila_to($row['pid'],1);
				else  return $row['tx_templavoila_to'];
			}
		}
	}
	function get_pagetree($uid)
	{
		$tree = t3lib_div::makeInstance('t3lib_pageTree');
		$tree->init('AND '.$GLOBALS['BE_USER']->getPagePermsClause(1));
		$cos = $tree->getTree($uid, 9, '');
		$alltree=$tree->tree;
		return $alltree;
	}
function add_file($file,$dam)
	{
		$loc=$_SERVER['DOCUMENT_ROOT'];
		if($loc!='/' && substr($loc,-1)!='/') $loc=$_SERVER['DOCUMENT_ROOT'].'/';
		$add=array();
		/*
		$where = 'hidden=0 AND deleted=0 AND uid IN('.$file.')';
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('title,path,base,uid','sys_filemounts',$where);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			if($row['base']==1)
			{

				$path=$loc.'fileadmin/'.$row['path'];
			}
			//print_r($path);
			if($dam!=1) $add[]='<li><a href="/typo3/file_list.php?id='.urlencode($path).'">'.$row['title'].'</a></li>';	
			else $add[]='<li><a href="/typo3conf/ext/dam/mod_file/index.php?&SLCMD[SELECT][txdamFolder]['.urlencode($path).']=1" target="_blank">'.$row['title'].'</a></li>';	
		}
		*/
		if($GLOBALS['BE_USER']->user['admin']==1)
		{
		
			$tx_templavoila_to=$this->get_tx_templavoila_to($GLOBALS['TSFE']->id);
			if($tx_templavoila_to!='' || $tx_templavoila_to!=0)
			{
				$where = 'deleted=0 AND uid='.$tx_templavoila_to;
				$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('fileref','tx_templavoila_tmplobj',$where);
				while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
				{
					$file=str_replace('.cachetmp','',$row['fileref']);
					$exp=explode('/',$file);
					$exp[count($exp)-1]='view_'.$exp[count($exp)-1];
					$view=implode('/',$exp);
					
					$add[]='<li><a href="/typo3/file_edit.php?target='.urlencode($file).'">Edytuj szablon</a></li>';
					if(file_exists($view)) $add[]='<li><a href="/typo3/file_edit.php?target='.urlencode($view).'">Edytuj VIEW</a></li>';
					$add[]='<li><a href="/typo3/mod.php?&M=web_txmtssM1&SET[function]=2&file='.urlencode($file).'">Aktualizuj szablon</a></li>';
					
				}
			}
			
			$locfile=$loc.'fileadmin/templates/'.$this->conf['path'];
			//$add[]='<li><a href="/typo3/file_list.php?id='.urlencode($locfile.'/css/').'">CSS</a></li>';
			//$add[]='<li><a href="/typo3/file_list.php?id='.urlencode($locfile.'/js/').'"  class="open_new" target="_blank">JS</a></li>';
			//$add[]='<li><a href="/typo3/file_list.php?id='.urlencode($locfile.'/images/').'"  class="open_new" target="_blank">Images</a></li>';
		}
		
		return '<div id="fileme"><a href="#"><span></span>Pliki</a><ul>'.implode("\n",$add).'</ul></div>';
	}
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/jh24_panel/pi1/class.tx_jh24panel_pi1.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/jh24_panel/pi1/class.tx_jh24panel_pi1.php']);
}

?>